using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using TONLI.TJOA.Common;
using TONLI.TJOA.Domain;
using ComponentArt.Web.UI;
using TONLI.Domain.RightManager;
namespace TONLI.TJOA.Anquan {
    public partial class AQK_XXCKHBZ : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack && !FolderGrid.IsCallback) {
                StrWhere = null;
                
                AjaxPro.Utility.RegisterTypeForAjax(typeof(AQK_XXCKHBZ));

                if (Session["user"] != null) {
                    Smmuser user = Session["user"] as Smmuser;
                    if (user.CorpDpt.StartsWith("002")) {
                        StrWhere = " dwmc='" + user.Dwmc + "' order by index_ ";
                    } else {
                        treecontent1.Width = 150;
                        buildTree();
                    }
                }
                BuildFolderGrid();
                FolderGrid.DataBind();
                
            }
            
        }

        #region webMethod
        [AjaxPro.AjaxMethod]
        public TJOA_AQK_XXCKHBZ GetNewWF_HD() {
            return new TJOA_AQK_XXCKHBZ();
        }
        [AjaxPro.AjaxMethod]
        public int AddWF_HD(TJOA_AQK_XXCKHBZ obj) {
            if(obj.DWMC.Trim()=="")return 0;
            if (obj.USERNAME.Trim() == "") return 0;
            DataService.BaseService().Create<TJOA_AQK_XXCKHBZ>(obj);
            return 1;
        }
        [AjaxPro.AjaxMethod]
        public int DelWF_HD(string id) {
            TJOA_AQK_XXCKHBZ obj = new TJOA_AQK_XXCKHBZ();
            obj.SUID = id;
            DataService.BaseService().Delete<TJOA_AQK_XXCKHBZ>(obj);
            return 1;
        }
        [AjaxPro.AjaxMethod]
        public TJOA_AQK_XXCKHBZ GetWF_HD(object obj) {
            TJOA_AQK_XXCKHBZ ret = DataService.BaseService().GetOneByKey<TJOA_AQK_XXCKHBZ>(obj);
            return ret;
        }
        [AjaxPro.AjaxMethod]
        public int UpdateWF_HD(TJOA_AQK_XXCKHBZ obj) {
            DataService.BaseService().Update<TJOA_AQK_XXCKHBZ>(obj);
            return 1;
        }
        #endregion
        private void buildTree() {
            treeview1.Nodes.Clear();
            TreeViewNode node1 = null;
            Hashtable hs = new Hashtable();
            hs.Add("p1", "deptname");
            hs.Add("p2", "SmmDept where left(deptdm,3)='002' order by deptdm");
            IList list = DataService.BaseService().GetList("SelectTJOA_AQK_XXJKHBZ_dwmcList", hs);
            foreach (TJOA_AQK_XXJKHBZ obj in list) {
                node1 = new TreeViewNode();
                node1.ID = obj.DWMC;
                node1.Text = obj.DWMC;
                node1.ImageUrl = "file.gif";
                treeview1.Nodes.Add(node1);
            }
        }
        private void BuildFolderGrid() {
            FolderGrid.DataSource = DataService.BaseService().GetList("SelectTJOA_AQK_XXCKHBZListByWhere", StrWhere);
        }
        public string StrWhere {
            get { return Session["TJOA_AQK_XXCKHBZ_Parentid"] == null ? "1=1 order by index_" : Session["TJOA_AQK_XXCKHBZ_Parentid"].ToString(); }
            set { Session["TJOA_AQK_XXCKHBZ_Parentid"] = value; }
        }
        bool flage1 = false;
        private int curPage {
            get { return Session["TJOA_AQK_XXCKHBZ_page"] == null ? 0 : (int)Session["TJOA_AQK_XXCKHBZ_page"]; }
            set { Session["TJOA_AQK_XXCKHBZ_page"] = value; }
        }
        protected void FolderGrid_NeedRebind(object sender, EventArgs e) {
            FolderGrid.DataBind();
            FolderGrid.CurrentPageIndex = curPage;
        }

        protected void FolderGrid_NeedDataSource(object sender, EventArgs e) {
            BuildFolderGrid();
        }

        protected void FolderGrid_PageIndexChanged(object sender, ComponentArt.Web.UI.GridPageIndexChangedEventArgs e) {
            FolderGrid.CurrentPageIndex = e.NewIndex;
            
            curPage = e.NewIndex;
        }

        protected void FolderGrid_SortCommand(object sender, ComponentArt.Web.UI.GridSortCommandEventArgs e) {
            FolderGrid.Sort = e.SortExpression;
        }

        public void FolderCallBack_Callback(object sender, ComponentArt.Web.UI.CallBackEventArgs args) {
            if (!string.IsNullOrEmpty(args.Parameter)) {
                StrWhere = " dwmc='" + args.Parameter + "' order by index_ "; 
                BuildFolderGrid();
            }
            FolderGrid.DataBind();
            
            FolderGrid.RenderControl(args.Output);
        }
    }
}